package com.yuyou.weixin.service.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.yuyou.weixin.vo.resp.UserInfoExcel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yuyou.weixin.entity.SrsActivity;
import com.yuyou.weixin.entity.SrsActivityUserRelation;
import com.yuyou.weixin.entity.SrsUser;
import com.yuyou.weixin.enums.ActivityEnums;
import com.yuyou.weixin.mapper.SrsActivityMapper;
import com.yuyou.weixin.service.ISrsActivityService;
import com.yuyou.weixin.service.ISrsActivityUserRelationService;
import com.yuyou.weixin.service.ISrsUserService;
import com.yuyou.weixin.util.Sequence;
import com.yuyou.weixin.vo.req.ReqAddPrize;
import com.yuyou.weixin.vo.req.ReqCloseActivity;
import com.yuyou.weixin.vo.resp.RespActivityHome;

@Service
public class SrsActivityServiceImpl implements ISrsActivityService{

	@Autowired
	private SrsActivityMapper srsActivityMapper;
	
	@Autowired
	private ISrsUserService srsUserService;

	@Autowired
	private ISrsActivityUserRelationService SrsActivityUserRelationService;

	@Value("${active.count}")
	private Integer activeCount;
	
	@Override
	public int deleteByPrimaryKey(Long srsActivityId) {
		return srsActivityMapper.deleteByPrimaryKey(srsActivityId);
	}

	@Override
	public int insert(SrsActivity record) {
		record.setSrsActivityId(Sequence.getSequence().nextId());
		record.setCreateTime(new Date());
		return srsActivityMapper.insert(record);
	}

	@Override
	public SrsActivity selectByPrimaryKey(Long srsActivityId) {
		return srsActivityMapper.selectByPrimaryKey(srsActivityId);
	}

	@Override
	public SrsActivity selectBySrsUserId(Long srsUserId, String projectCode) {
		return srsActivityMapper.selectBySrsUserId(srsUserId,projectCode);
	}

	@Override
	public int updateByPrimaryKeySelective(SrsActivity record) {
		return srsActivityMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	@Transactional
	public RespActivityHome home(Long srsActivityId,String openId,String headImg,String name,String projectCode) {
		srsUserService.addOrUpUser(openId, headImg ,name,projectCode);
		RespActivityHome res=new RespActivityHome();
					
		SrsActivity srsActivity=this.selectByPrimaryKey(srsActivityId);	
		//当前人
		SrsUser currUser=srsUserService.selectByOpenId(openId,projectCode);
		
		//当前人是否存在活动
		SrsActivity selfActivitu=this.selectBySrsUserId(currUser.getUserId(),projectCode);
		if(selfActivitu==null){
			res.setHasActivity(false);
		}else{
			res.setHasActivity(true);
		}
		
		//进入自己活动
		if(srsActivity.getSrsUserId().equals(currUser.getUserId())){
			res.setMySelf(true);
			if(srsActivity.getStatus().equals(ActivityEnums.status.init.getValue())){
				res.setStatus(ActivityEnums.homeStatus.home.getValue());
			}
			else if(srsActivity.getStatus().equals(ActivityEnums.status.finish.getValue())){
				res.setStatus(ActivityEnums.homeStatus.prize.getValue());
			}
			else{
				res.setStatus(ActivityEnums.homeStatus.prized.getValue());
			}			
		}
		//进入别人活动
		else{
			res.setMySelf(false);
			//是否已经助力
			SrsActivityUserRelation rel=SrsActivityUserRelationService.selectBySrsUserId(currUser.getUserId(),currUser.getProjectCode());
			if(rel==null){
				//助力
				SrsActivityUserRelation addRel=new SrsActivityUserRelation();
				addRel.setSrsActivityId(srsActivityId);
				addRel.setSrsUserId(currUser.getUserId());
				addRel.setProjectCode(projectCode);
				SrsActivityUserRelationService.insert(addRel);
				res.setStatus(ActivityEnums.homeStatus.home.getValue());
				
				
				List<SrsUser> helpList=srsUserService.findImgByActivityId(srsActivityId);
				//判断活动是否已经达标，更新活动状态
				if(helpList.size()>=activeCount && srsActivity.getStatus().equals(ActivityEnums.status.init.getValue())){
					SrsActivity updateSrs=new SrsActivity();
					updateSrs.setSrsActivityId(srsActivity.getSrsActivityId());
					updateSrs.setStatus(ActivityEnums.status.finish.getValue());
					this.updateByPrimaryKeySelective(updateSrs);
				}				
			}else{
				res.setStatus(ActivityEnums.homeStatus.homeHelp.getValue());
			}
		}
		
			
		//活动发起数据
		SrsUser srsUser=srsUserService.selectByPrimaryKey(srsActivity.getSrsUserId());
		res.setActivityName(srsActivity.getActivityName());
		res.setSrsUserName(srsUser.getName());
		res.setSrsUserHeadImg(srsUser.getHeadImg());
		
		List<SrsUser> helpList=srsUserService.findImgByActivityId(srsActivityId);
		res.setHelpHeadImgTotal(helpList.size());
		List<String> headList=helpList.stream().map(item->item.getHeadImg()).collect(Collectors.toList());
		res.setHelpHeadImgList(headList);	
				
		return res;
	}

	
	private SrsActivity addOrUp(String openId,String headImg,String name,String projectCode){
		SrsUser currUser=srsUserService.selectByOpenId(openId,projectCode);
		SrsActivity srsActivity=this.selectBySrsUserId(currUser.getUserId(),projectCode);
		if(srsActivity==null){
			srsActivity=new SrsActivity();
			srsActivity.setSrsUserId(currUser.getUserId());
			srsActivity.setStatus(ActivityEnums.status.init.getValue());
			srsActivity.setProjectCode(projectCode);
			this.insert(srsActivity);
		}
		return srsActivity;
	}
	
	@Override
	@Transactional
	public Long myActivity(String openId,String headImg,String name,String projectCode) {
		srsUserService.addOrUpUser(openId, headImg ,name,projectCode);
		addOrUp(openId,headImg,name,projectCode);
		return addOrUp(openId,headImg,name,projectCode).getSrsActivityId();
	}

	@Override
	@Transactional
	public boolean prize(ReqAddPrize reqAddPrize) throws Exception {
			
		SrsUser activityUser=srsUserService.selectByOpenId(reqAddPrize.getOpenId(),reqAddPrize.getProjectCode());			
		SrsActivity srsActivity=this.selectBySrsUserId(activityUser.getUserId(),activityUser.getProjectCode());
		
		if(srsActivity.getStatus().equals(ActivityEnums.status.prized.getValue())){
			return true;
		}
		
		Integer count = findActiveCountBYOpenId(reqAddPrize.getOpenId(),reqAddPrize.getProjectCode());
		if(count == null || count < activeCount){//验证激活次数是否符合要求
			throw new Exception("助力次数未达标");
		}else if(!srsActivity.getStatus().equals(ActivityEnums.status.finish.getValue())){
			throw new Exception("助力次数未达标");
		}
		//更新领取数据
		updatePrizeByOpenId(reqAddPrize);


		return true;
	}

	@Override
	public List<Long> findCompletedNoUpdate(Integer activeCount) {
		return srsActivityMapper.findCompletedNoUpdate(activeCount);
	}

	@Override
	public void updateByIds(List<Long> list) {
		srsActivityMapper.updateByIds(list);
	}

	@Override
	public Integer findActiveCountBYOpenId(String openId,String projectCode) {
		return srsActivityMapper.findActiveCountBYOpenId(openId,projectCode);
	}

	@Override
	public void updatePrizeByOpenId(ReqAddPrize reqAddPrize) {
		srsActivityMapper.updatePrizeByOpenId(reqAddPrize);
	}

	@Override
	public void closeByOpenId(ReqCloseActivity reqCloseActivity) {
		srsActivityMapper.closeByOpenId(reqCloseActivity);
	}
	
	
	@Override
	public List<UserInfoExcel> findUserInfo(String projectCode) {
		return srsActivityMapper.findUserInfo(projectCode);
	}

	@Override
	public boolean closeActivity(ReqCloseActivity reqCloseActivity)
			throws Exception {
		SrsUser activityUser=srsUserService.selectByOpenId(reqCloseActivity.getOpenId(),reqCloseActivity.getProjectCode());			
		SrsActivity srsActivity=this.selectBySrsUserId(activityUser.getUserId(),activityUser.getProjectCode());
		
		if(srsActivity.getStatus().equals(ActivityEnums.status.prized.getValue())){
			return true;
		}
		
		Integer count = findActiveCountBYOpenId(reqCloseActivity.getOpenId(),reqCloseActivity.getProjectCode());
		if(count == null || count < activeCount){//验证激活次数是否符合要求
			throw new Exception("助力次数未达标");
		}else if(!srsActivity.getStatus().equals(ActivityEnums.status.finish.getValue())){
			throw new Exception("助力次数未达标");
		}
		
		
		//更新领取数据
		closeByOpenId(reqCloseActivity);


		return true;
	}

}
